package com.rahms.server.controller;

import com.rahms.common.constant.MessageConstant;
import com.rahms.common.result.R;
import com.rahms.pojo.entity.ElderlyInfo;
import com.rahms.server.service.ElderlyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人信息控制器
 */
@RestController
@RequestMapping("/elderly")
public class ElderlyInfoController {
    
    @Autowired
    private ElderlyInfoService elderlyInfoService;
    
    /**
     * 获取用户关联的老人列表
     */
    @GetMapping("/list")
    public R<List<ElderlyInfo>> getElderlyListByUserId(@RequestParam Long userId) {
        List<ElderlyInfo> list = elderlyInfoService.getElderlyListByUserId(userId);
        return R.success(list);
    }
    
    /**
     * 获取老人详情
     */
    @GetMapping("/detail")
    public R<ElderlyInfo> getElderlyDetail(@RequestParam Long elderlyId) {
        ElderlyInfo elderlyInfo = elderlyInfoService.getById(elderlyId);
        return elderlyInfo != null ? R.success(elderlyInfo) : R.error(MessageConstant.ELDERLY_NOT_EXIST);
    }
    
    /**
     * 添加老人信息
     */
    @PostMapping("/add")
    public R<Boolean> addElderlyInfo(@RequestBody ElderlyInfo elderlyInfo) {
        boolean result = elderlyInfoService.save(elderlyInfo);
        return result ? R.success(true).add("message", MessageConstant.ELDERLY_ADD_SUCCESS) : R.error(MessageConstant.SAVE_FAILED);
    }
    
    /**
     * 更新老人信息
     */
    @PostMapping("/update")
    public R<Boolean> updateElderlyInfo(@RequestBody ElderlyInfo elderlyInfo) {
        boolean result = elderlyInfoService.updateById(elderlyInfo);
        return result ? R.success(true).add("message", MessageConstant.ELDERLY_UPDATE_SUCCESS) : R.error(MessageConstant.UPDATE_FAILED);
    }
    
    /**
     * 删除老人信息
     */
    @PostMapping("/delete")
    public R<Boolean> deleteElderlyInfo(@RequestParam Long elderlyId) {
        boolean result = elderlyInfoService.removeById(elderlyId);
        return result ? R.success(true).add("message", MessageConstant.ELDERLY_DELETE_SUCCESS) : R.error(MessageConstant.DELETE_FAILED);
    }
}